<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>

	<body>
		<h1>属性</h1>
		<p id="d1">
			
		</p>
		<script type="text/javascript">
			var p = {
				x:1.0,
				y:1.0,
				//r是可读写属性 
				get r(){
					return Math.sqrt(this.x*this.x + this.y*this.y);
				},
				set r(newValue){
					var oldValue = 	Math.sqrt(this.x*this.x + this.y*this.y);
					var ratio = newValue/oldValue;
					this.x = ratio;
					this.y = ratio;
				},
				//theta是只读属性 
				get theta(){
					return Math.atan2(this.y,this.x);
				}
			};
			var d1 = document.getElementById("d1");
			window.onload = function(){
				var x = {};
				//修改对象属性 				
				Object.defineProperty(x,"a",{value:1,writable:true,enumerable:false,configurable:true});
				//若要修改多个属性可以使用defineProperties()
				//对于那些不能修改属性的对象来说调用这两个方法都会抛出类型错误异常
// 				Object.defineProperties({},{
// 					a:{},
// 					b:{},
// 					c:{}
// 				});
				
				x.a = 3;
				console.log(x.a);//3
				Object.defineProperty(x,"a",{writable:false});
				x.a = 2;//此句无效
				console.log(x.a);//3
				console.log(Object.getOwnPropertyDescriptor(x,"a"));//Object { configurable=true, enumerable=false, value= 3 , 更多...}
				console.log(p.r);
				console.log(p.theta);
				//原型属性 
				console.log(x.__proto__);//Object{} 这个是FF定义的属性 
				console.log(Object.getPrototypeOf(x));//Object{}
				console.log(x.constructor.prototype);//Object{} 通过new创建对象会继承constructor属性
				console.log(Object.prototype.isPrototypeOf(x));// true
				var y = Object.create(x);
				console.log(x.isPrototypeOf(y)); //true 表示x是y的原型
				//类属性 
				//toString获取对象的类型信息
				console.log(d1.toString());
				var n1 = 1;
				//有时要用原型的toString,自带的覆盖了原型的
				//取8至倒数第二位是为了截取类型,不截取返回的是： [Object Number]
				console.log(Object.prototype.toString.call(n1).slice(8,-1));//Number;
				//可扩展性
				//preventExtensions()、seal()、freeze()//都会返回传入对象
				Object.seal(x);
				console.log(Object.isSealed(x));//true
				Object.preventExtensions(x);
				console.log(Object.isExtensible(x));//false
				Object.freeze(x);
				console.log(Object.isFrozen(x));//true
				//序列号对象 [JSON  JavaScript Object Notation]
				//JSON.stringify(), JSON.parse()
				var json1 = {x:1,y:{a:2,b:3}};
				console.log(json1); // Object { x=1,  y={...}}
				//序列化为JSON对象
				var j1 = JSON.stringify(json1);
				console.log(j1); //{"x":1,"y":{"a":2,"b":3}}
				//还原为Js对象
				var j2 = JSON.parse(j1);
				console.log(j2); //Object { x=1,  y={...}}

				var s1 = "h w";
				console.log(s1.valueOf());
				
			};
		</script>
	</body>

</html>